package com.atguigu.gulimail.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.vo.AttrGroupAndAttrsVo;
import com.atguigu.gulimail.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author xujiale
 * @email 2519970360@qq.com
 * @date 2023-10-24 15:34:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;
///product/attrgroup/{catelogId}/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable Long catelogId){

        List<AttrGroupAndAttrsVo> attrGroupAndAttrsVo=attrGroupService.getAttrGroupAndAttrs(catelogId);

        return R.ok().put("data",attrGroupAndAttrsVo);
    }

    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveRelations(vos);
        return R.ok();
    }

    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable Long attrgroupId,@RequestParam Map<String,Object> param){

        PageUtils page=attrService.getNoRelation(param,attrgroupId);

        return R.ok().put("page",page);
    }

    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){

        List<AttrAttrgroupRelationEntity> collect =attrGroupRelationVos.stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println(attrGroupRelationVos);

        attrGroupService.deleteRelations(collect);

        return R.ok();
    }


    /**
     * 获取分组的所有参数
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> attrRelation = attrGroupService.getAttrRelation(attrgroupId);
        return R.ok().put("data",attrRelation);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{cateId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long cateId){
        PageUtils page = attrGroupService.queryPage(params,cateId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getInfo(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
